layui.use(['form','laypage','layer'],function(){
	
	var form = layui.form
	,layer = layui.layer
	,laypage = layui.laypage;
	
	  form.on('checkbox(selectall-btn)',function(){selectAll();});
	  form.on('checkbox(selectfx-btn)',function(){selectFx();})
	  $('#save-btn').on('click',function(){save();});
	  initPage();
	  
	  
	  function getData(pageIndex,pageSize){
	 	  var data = {};
	 	  data["pageIndex"] = pageIndex?pageIndex:1;
	 	  data["pageSize"] = pageSize?pageSize:10;
	 	  var caseName = $("#caseName").val();
	 	  data["caseName"] = caseName;
	 	  var appId = $("#appId").val();
	 	  data["appId"] = appId;
	 	  return data;
	   	}
	  
	  
	  function initPage(){
		  var data = getData();
		  ajaxPost("../case/laylist",data,
				  function(data){
			  createData(data.data.list);
			  laypage.render({
				  elem:'page',
				  count:data.data.total,
				  layout: ['count', 'prev', 'page', 'next', 'limit', 'skip']
			    ,jump: function(obj,first){
			    	if(!first){
			    		toPage(obj.curr,obj.limit);
			    	}
			    }
			  });
		  },
				  function(data){
					  layer.alert(data.errorMsg,{
							icon:5,
							title:"错误"
						});
				  });
	  }
	  
	  window.initPage = initPage;
	  function toPage(pageIndex,pageSize){
	 	  
	 	var data = getData(pageIndex,pageSize);
	 	  ajaxPost("../case/laylist",data,
	 			  function(data){createData(data.data.list);},
	 			  function(data){
	 				  layer.alert(data.errorMsg,{
	 						icon:5,
	 						title:"错误"
	 					});
	 			  });
	   }
	  
	  
	  function createData(data){
		  var caseIds = $("#caseIds").val().split(",");
		  if(isBlank(caseIds))
			  caseIds = "";
		  var content = [];
		  for(var i =0 ;i < data.length;i++){
			  var casedata = data[i];
			  var id = casedata.id;
			  content.push('<tr>');
			  if(caseIds.indexOf(id+"") > -1)
				  content.push('<td><input type="checkbox" name="checkbox"  lay-skin="primary" value="'+casedata.id+'" checked/></td>');
			  else
				  content.push('<td><input type="checkbox" name="checkbox"  lay-skin="primary" value="'+casedata.id+'"/></td>');
			  content.push('<td>'+casedata.appName+'('+(casedata.platform == 1?"ANDRIOD":"IOS")+')</td>');
			  content.push('<td>'+casedata.caseName+'</td>');
			  content.push('<td>'+(casedata.caseType == 1?'正面':'负面')+'</td>');
			  content.push('<td>'+(casedata.editStatus == 1?"有效":"删除")+'</td>');
			  content.push('</tr>');
		  }
		  
		  $("#dg").empty().append(content.join(""));
		  form.render('checkbox');
	  }
	  
	  
	  
	  function save(){
		  
		  var seldata = [];
		  $("input[name='checkbox']:checked").each(function(){
			  var id = $(this).val();
			  var closestTr = $(this).closest("tr");
			  var appName = closestTr.find("td:eq(1)").text();
			  var caseName = closestTr.find("td:eq(2)").text();
			  var caseType = closestTr.find("td:eq(3)").text();
			  seldata.push({"caseId":id,"caseName":caseName,"appName":appName,"caseType":caseType});
			  
		  });
		  
		  if(seldata.length == 0){
				  layer.alert("请选择一条案例",{
						icon:5,
						title:"错误"
					});
			  return false;
		  }
		  
		  parent.selCase(seldata);
		  var idx = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
		  parent.layer.close(idx);
	  }
	  
	 function selectAll(){
		 $("input[name='checkbox']").prop("checked",$("#selectAll").prop("checked"));
		 form.render("checkbox");
	 }
	 
	 function selectFx(){
		 $("input[name='checkbox']").each(function(){
			 if($(this).prop("checked"))
				 $(this).prop("checked",false);
			 else
				 $(this).prop("checked",true);
		 });
		 form.render("checkbox");
	 }
	  
});